﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Geometry
{
	public class GeomVector2
	{
		private static readonly float errorAllowedComparation = (float)Math.Pow(10, -2);

		private float x;
		private float y;

		public float X
		{
			get { return x; }
			set { x = value; }
		}

		public float Y
		{
			get { return y; }
			set { y = value; }
		}

		public GeomVector2(float x, float y)
		{
			this.x = x;
			this.y = y;
		}

		public GeomVector2(GeomVector2 vector)
		{
			this.x = vector.X;
			this.y = vector.Y;
		}

		public float getNorm()
		{
			return (float)(Math.Pow(Math.Pow(x, 2) + Math.Pow(y, 2), 1 / 2F));
		}

		public void Normalize()
		{ 
			float norm = getNorm();

			if (norm != 0)
			{
				X = X / norm;
				Y = Y / norm;
			}
		}

		static public float Distance(GeomVector2 vectorA, GeomVector2 vectorB)
		{
			GeomVector2 substraction = new GeomVector2(vectorA.X - vectorB.X, vectorA.Y - vectorB.Y);

			return substraction.getNorm();
		}

		public static bool operator ==(GeomVector2 a, GeomVector2 b)
		{
			bool xEqual = (a.X < b.X + errorAllowedComparation && a.X > b.X - errorAllowedComparation) ? true : false;
			bool yEqual = (a.Y < b.Y + errorAllowedComparation && a.Y > b.Y - errorAllowedComparation) ? true : false;

			return xEqual && yEqual;
		}

		public static bool operator !=(GeomVector2 a, GeomVector2 b)
		{
			return !(a == b);
		}

		public override string ToString()
		{
			return "X:" + x.ToString() + " Y:" + y.ToString();
		}
	}
}
